package com.kejiguan.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.kejiguan.ui.adapter.DisplayItemAdapter;
import com.kejiguan.ui.adapter.DisplayMenuAdapter;
import com.kejiguan.ui.event.RefreshNeStateEvent;
import com.kejiguan.ui.model.DisplayArea;
import com.kejiguan.ui.presenter.NeItemRealMonitorPresenter;
import com.kejiguan.ui.response.GetNeItemInfosResponse;
import com.kejiguan.ui.util.CacheDataUtil;
import com.kejiguan.ui.util.GridSpacingItemDecoration;
import com.kejiguan.ui.util.ScreenUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Iterator;
import java.util.Set;

/**
 * Created by YYY on 2016/5/1.
 */
public class NeItemRealMonitorFragment extends Fragment implements View.OnClickListener{

    private View rootView;
    private RecyclerView displayMenuList;
    private RecyclerView displayItemList;

    private int currentPosition=0;
    private DisplayMenuAdapter menuAdapter;
    private DisplayItemAdapter adapter;

    private NeItemRealMonitorPresenter mPresenter;

    public void onCreate(Bundle bundle){
        EventBus.getDefault().register(this);
        mPresenter=new NeItemRealMonitorPresenter();
        mPresenter.attach(getContext());
    }


    @Override
    public final View onCreateView(LayoutInflater inflater, ViewGroup container,
                                   Bundle savedInstanceState) {

        if (rootView == null) {

            rootView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_realtime_monitor_layout, container, false);
            displayMenuList = (RecyclerView) rootView.findViewById(R.id.display_menu_list);
            displayItemList = (RecyclerView) rootView.findViewById(R.id.display_item_list);

            LinearLayoutManager displayAreaLayoutManager=new LinearLayoutManager(getContext());
            displayAreaLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            displayMenuList.setLayoutManager(displayAreaLayoutManager);

            GridLayoutManager gridLayoutManager=new GridLayoutManager(getContext(),3);
            displayItemList.setLayoutManager(gridLayoutManager);
            displayItemList.addItemDecoration(new GridSpacingItemDecoration(3, ScreenUtils.dip2px(getContext(), 24), true));
        }
        return rootView;
    }

    @Override
    public void onResume()
    {
        super.onResume();
        if(null==menuAdapter)
        {
            mPresenter.getNeItemInfos();
        }else
        {
            if (null!=adapter)
            {
                adapter.notifyDataSetChanged();
            }
        }
    }


    @Override
    public void onDestroy()
    {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.menu_name:
                int position= (int) v.getTag(R.id.recyclerview_item_position);
                currentPosition=position;
                menuAdapter.setSelectArea(position);
                adapter.setPresenterItems(CacheDataUtil.getInstance().getDisplayAreas().get(position).getExhibitionNeItems());
                break;
        }


    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(GetNeItemInfosResponse response)
    {
        if ("0000".equals(response.getRetCode())&&!response.getExhibitionRecordMap().isEmpty())
        {
            Set<String> keys=response.getExhibitionRecordMap().keySet();
            Iterator<String> stringIterator=keys.iterator();
            while (stringIterator.hasNext())
            {
                String key=stringIterator.next();
                String[] mutyKeys=key.split("=");

                if (CacheDataUtil.getInstance().getDisPlayAreaIdNames().contains(key))
                {

                    int disPalyAreaCount=CacheDataUtil.getInstance().getDisplayAreas().size();
                    for (int i=0;i<disPalyAreaCount;i++)
                    {
                        if (mutyKeys[0].equals(CacheDataUtil.getInstance().getDisplayAreas().get(i).getDisplayId()))
                        {

                            CacheDataUtil.getInstance().getDisplayAreas().get(i).setExhibitionNeItems(response.getExhibitionRecordMap().get(key));
                        }
                    }

                }else//新增操作
                {

                    DisplayArea displayArea=new DisplayArea();
                    displayArea.setDisplayId(mutyKeys[0]);
                    displayArea.setDisplayName(mutyKeys[1]);
                    displayArea.setExhibitionNeItems(response.getExhibitionRecordMap().get(key));

                    CacheDataUtil.getInstance().getDisplayAreas().add(displayArea);
                    CacheDataUtil.getInstance().getDisPlayAreaIdNames().add(key);
                }
            }

            menuAdapter=new DisplayMenuAdapter(getContext(), CacheDataUtil.getInstance().getDisplayAreas());
            menuAdapter.setOnClickListener(this);
            displayMenuList.setAdapter(menuAdapter);

            adapter = new DisplayItemAdapter(getContext(), CacheDataUtil.getInstance().getDisplayAreas().get(0).getExhibitionNeItems());
            adapter.setOnClickListener(this);
            displayItemList.setAdapter(adapter);

            EventBus.getDefault().post(new RefreshNeStateEvent());
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(RefreshNeStateEvent event)
    {

        menuAdapter.notifyDataSetChanged();
        adapter.setPresenterItems(CacheDataUtil.getInstance().getDisplayAreas().get(currentPosition).getExhibitionNeItems());
    }

}
